package com.qk.management.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.management.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/activities")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result page(
            @RequestParam(name="channel",required = false)Integer channel,
            @RequestParam(name="type",required = false)Integer type,
            @RequestParam(name="status",required = false)Integer status, // 修改为required = false
            @RequestParam(name="page",defaultValue = "1")Integer page,
            @RequestParam(name="pageSize",defaultValue = "10")Integer pageSize
    ){
        log.info("分页查询活动,,参数:活动渠道={},活动类型={},活动状态={},当前页码={},每页大小={}", channel, type, status, page, pageSize);
        PageResult<Activity> pageResult = activityService.page(channel, type, status, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除活动：{}",id);
        activityService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Activity activity) {
        log.info("添加活动：{}",activity);
        activityService.add(activity);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询活动：{}",id);
        Activity activity = activityService.findById(id);
        return Result.success(activity);
    }

    @PutMapping
    public Result update(@RequestBody Activity activity) {
        log.info("修改活动：{}",activity);
        activity.setUpdateTime(LocalDateTime.now());
        activityService.update(activity);
        return Result.success();
    }

    @GetMapping("/type/{type}")
    public Result findByType(@PathVariable Integer type) {
        log.info("查询活动：{}",type);
        List<Activity> activities = activityService.findByType(type);
        return Result.success(activities);
    }
}